/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-03-06 08:46:20
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-26 17:47:49
 * @FilePath: /yt-devops/lib/pages/common/select_page/select_page_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sprintf/sprintf.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/card_wrap.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/paging/refresh.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/utils/debounce_utils.dart';
import '../../../styles/styles.dart';
import 'select_page_controller.dart';

class SelectPagePage<T> extends GetWidget<SelectPageController<T>> {
  @override
  final String? tag = RouterPath.selectPage;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        FocusScope.of(context).unfocus();
      },
      child: GetBuilder<SelectPageController<T>>(
        tag: tag,
        builder: (controller) => Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBarWidget.defaultAppBar(sprintf(
              ResKey.pleaseSelectSentence.tr, [controller.props.dataName])),
          // appBar: AppBarWidget.defaultAppBar(
          //     '${ResKey.pleSelect.tr} ${ResKey.site.tr}'),
          body: Column(
            children: [
              if (controller.props.isSearch)
                BrnSearchText(
                  maxHeight: 52.h,
                  innerPadding: EdgeInsets.only(
                      top: 8.h, bottom: 8.h, left: 20.w, right: 20.w),
                  controller: controller.textController,
                  innerColor: Colour.backgroundColor,
                  hintText: sprintf(ResKey.pleaseEnterSentence.tr,
                      [controller.props.dataName]),
                  // hintText:  '${ResKey.pleSelect.tr} ${ResKey.site.tr}',
                  onTextChange: DebounceUtils.debounceInput(
                      (text) => controller.refeshHandler(text), 500),
                  onTextCommit: (String text) => controller.refreshData(),
                  onTextClear: () {
                    controller.refreshData();
                    return false;
                  },
                ),
              Expanded(
                child: buildRefreshWidget(
                    refreshController: controller.refreshController,
                    enablePullUp: controller.pagingState.hasMore,
                    onRefresh: controller.refreshData,
                    onLoad: controller.loadMoreData,
                    builder: () {
                      return ListView(
                        padding: EdgeInsets.only(bottom: 20.h),
                        children: [
                          if (!controller.isLoad)
                            const SizedBox.shrink()
                          else if (controller.pagingState.isNoData)
                            ListEmptyWidget(topPosition: 82)
                          else
                            CardWrap(
                              child: BrnPortraitRadioGroup.withSimpleList(
                                options: controller.names,
                                selectedOption: controller.selectedNames.isEmpty
                                    ? ''
                                    : controller.selectedNames[0],
                                onChanged: (BrnPortraitRadioGroupOption? old,
                                    BrnPortraitRadioGroupOption? newList) {
                                  if (newList != old) {
                                    var index = controller.names.indexWhere(
                                        (element) => element == newList?.title);
                                    if (index > -1) {
                                      Get.back(
                                          result: controller
                                              .pagingState.data[index]);
                                    }
                                  }
                                },
                              ),
                            ),
                        ],
                      );
                    }),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
